package io.renren.modules.live.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.FastDFSClientUtils;
import io.renren.common.utils.PageResult;
import io.renren.modules.live.constants.ResourcesConstants;
import io.renren.modules.live.entity.FanweVideoEntity;
import io.renren.modules.live.entity.ResourceBean;
import io.renren.modules.live.vo.PropListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.live.entity.FanwePropEntity;
import io.renren.modules.live.service.FanwePropService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


/**
 * 礼物列表
 *
 * @author shaoqi
 * @email sunlightcs@gmail.com
 * @date 2020-11-17 09:18:22
 */
@Log4j2
@Api(tags = "礼物管理")
@RestController
@RequestMapping("live/fanweprop")
public class FanwePropController {
    @Autowired
    private FanwePropService fanwePropService;

    /**
     * 列表
     */
    @ApiOperation("获取所有礼物信息")
    @GetMapping("/getProplist")
    public R getProplist( @ApiParam("当前页") Long page,
                          @ApiParam("每页显示数量")Long size){
        Page<FanwePropEntity> userPage = new Page<>(page, size);
        fanwePropService.page(userPage, null);
        PageResult<FanwePropEntity> result1 = new PageResult<>();
        result1.setTotal(userPage.getTotal());
        result1.setRows(userPage.getRecords());
        return R.ok("获取成功").put("pageResult",result1);
    }


    /**
     * 信息
     */
    @ApiOperation("根据礼物ID获取礼物信息")
    @GetMapping("/info")
    //@RequiresPermissions("live:fanweprop:info")
    public R info(@ApiParam("礼物ID") Long id){
		FanwePropEntity fanweProp = fanwePropService.getById(id);
        return R.ok().put("fanweProp", fanweProp);
    }





    /**
     * 修改
     */
    @ApiOperation("修改礼物信息")
    @PostMapping("/update")
    //@RequiresPermissions("live:fanweprop:update")
    public R update(@RequestBody FanwePropEntity fanweProp){
		fanwePropService.updateById(fanweProp);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("根据礼物ID集合批量删除礼物")
    @DeleteMapping("/delete")
    //@RequiresPermissions("live:fanweprop:delete")
    public R delete(@RequestBody Integer[] ids){
		fanwePropService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    private ResourceBean uploadFileUitl(MultipartFile file){
        String extName = "";
        String fileName = "";
        String originalFilename = file.getOriginalFilename();
        if(originalFilename.contains(".")) {
            //拆分文件路径
            String[] fileArray = originalFilename.split("\\.");
            //获取文件扩展名
            extName = fileArray[1];
            //获取文件名
            fileName = fileArray[0];
        }else {
            fileName = originalFilename;
        }
        byte[] bytes = null;
        try {
            bytes = file.getBytes(); //将文件转换成字节流形式
        } catch (IOException e) {
            log.info(e);
        }
        //调用上传文件的具体方法
        String fileId= FastDFSClientUtils.upload(bytes,extName);
        return new ResourceBean(ResourcesConstants.getResourcesUrl(fileId), fileName);
    }

    /**
     * 佟卢海 2020-11-19
     * 查询礼物列表集合liveAdministration:prop
     */
    @ApiOperation("获取全部礼物信息")
    @PostMapping("/selectPropLise")
    @RequiresPermissions("liveAdministration:prop")
    public R selectPropLise(@RequestBody PropListVo propListVo){
        R result=new R();
        try{
            if(propListVo.getPageIndex()==null || propListVo.getPageSize() ==null){
                result.put("code",9999);
                result.put("msg","页码页容不能为空！！");
                result.put("data",null);
                return result;
            }
            Map<String, Object> stringObjectMap = fanwePropService.selectPropLise(propListVo);
            result.put("data",stringObjectMap);
        }catch (Exception e){
            log.info(e);
            result.put("code",9999);
            result.put("msg","查询失败");
            result.put("data",null);
        }
        return result;
    }
    /**
     * 佟卢海 2020-11-19
     * 新建或更新礼物
     */
    @ApiOperation("添加/更新礼物")
    @PostMapping("/creatAndUpdateProp")
    @RequiresPermissions("liveAdministration:prop")
    public R creatAndUpdateProp(@RequestBody FanwePropEntity fanwePropEntity){
        R result=new R();
        try{
            if(StringUtils.isEmpty(fanwePropEntity.getName())){
                result.put("code",9999);
                result.put("msg","礼物名不能为空！！");
                result.put("data",null);
                return result;
            }
            if(fanwePropEntity.getDiamonds()==null){
                result.put("code",9999);
                result.put("msg","礼物金额不能为空！！");
                result.put("data",null);
                return result;
            }
            if(fanwePropEntity.getScore()==null){
                result.put("code",9999);
                result.put("msg","礼物积分不能为空！！");
                result.put("data",null);
                return result;
            }
            if(fanwePropEntity.getIsMuch()==null){
                result.put("code",9999);
                result.put("msg","礼物类型不能为空！！");
                result.put("data",null);
                return result;
            }
            if(fanwePropEntity.getSort()==null){
                result.put("code",9999);
                result.put("msg","礼物权重不能为空！！");
                result.put("data",null);
                return result;
            }
            Integer integer = fanwePropService.creatAndUpdateProp(fanwePropEntity);
            result.put("data",null);
        }catch (Exception e){
            log.info(e);
            result.put("code",9999);
            result.put("msg","操作失败！！");
            result.put("data",null);
        }
        return result;
    }
    /**
     * 佟卢海 2020-11-19
     * 查询礼物列表集合
     */
    @ApiOperation("查询礼物列表")
    @GetMapping("/updatePropIsEffect")
    @RequiresPermissions("liveAdministration:prop")
    public R updatePropIsEffect( String isEffect,String id){
        R result=new R();
        try{
            if(StringUtils.isEmpty(isEffect)){
                result.put("code",9999);
                result.put("msg","isEffect不能为空！！");
                result.put("data",null);
                return result;
            }
            if(StringUtils.isEmpty(id)){
                result.put("code",9999);
                result.put("msg","id不能为空！！");
                result.put("data",null);
                return result;
            }
            Integer integer = fanwePropService.updatePropIsEffect(isEffect,id);
        }catch (Exception e){
            log.info(e);
            result.put("code",9999);
            result.put("msg","查询失败");
            result.put("data",null);
        }
        return result;
    }
    /**
     * 佟卢海 2020-11-25
     * 通过id查询礼物信息
     */
    @GetMapping("/selectProp")
    @RequiresPermissions("liveAdministration:prop")
    public R selectProp(String id){
        R result=new R();
        try{
            if(StringUtils.isEmpty(id)){
                result.put("code",9999);
                result.put("msg","id不能为空！！");
                result.put("data",null);
                return result;
            }
            FanwePropEntity fanwePropEntity = fanwePropService.selectProp(id);
            result.put("fanwePropEntity",fanwePropEntity);
        }catch (Exception e){
            log.info(e);
            result.put("code",9999);
            result.put("msg","查询失败");
            result.put("data",null);
        }
        return result;
    }
    /**
     * 佟卢海 2020-11-23
     * 礼物添加
     */
    @PostMapping("/addProp")
    public R anchorUploadFile(@RequestParam MultipartFile muFile){
        R result=new R();
        try {
            ResourceBean fileUrlAndName = uploadFileUitl(muFile);
            result.put("url",fileUrlAndName.getFileUrl());
        }catch (Exception e){
            result.put("code",9999);
            result.put("msg","添加失败");
            log.info(e);
        }
        return result;
    }
}
